package com.dcy.znoa.service.dic.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.util.RedisClient;
import com.dcy.znoa.common.util.SerializeUtil;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.dao.dic.DicCityCodeDao;
import com.dcy.znoa.dao.dic.DicDistrictCodeDao;
import com.dcy.znoa.dao.dic.DicProvinceCodeDao;
import com.dcy.znoa.domain.dic.DicCityCode;
import com.dcy.znoa.domain.dic.DicDistrictCode;
import com.dcy.znoa.domain.dic.DicProvinceCode;
import com.dcy.znoa.service.dic.AreaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    DicProvinceCodeDao provinceCodeDao;
    @Autowired
    DicCityCodeDao cityCodeDao;
    @Autowired
    DicDistrictCodeDao districtCodeDao;


    @Override
    public JsonForList getAll() throws InstantiationException, IllegalAccessException, UnsupportedEncodingException {
        //查询缓存
//        Jedis jedis = RedisClient.getConnect();
//        byte[] provinceList_byte = jedis.get(RedisClient.ALL_AREA.getBytes());
//        jedis.close();
//        if(provinceList_byte!=null){
//            List<DicProvinceCode> provinceList_redis =   (List<DicProvinceCode>) SerializeUtil.unserialize(provinceList_byte);
//            json.setCode("200");
//            json.setMsg("ok");
//            json.setData(provinceList_redis);
//            return json;
//        }
        JsonForList json = RedisClient.getJsonListfromRedisOfByte(RedisClient.ALL_AREA);
        if (json != null) {
            return json;
        }
        List<DicProvinceCode> provinceList = provinceCodeDao.selectList(null);
        for (DicProvinceCode provice : provinceList) {
//            DicCityCode city_query = new DicCityCode();
//            city_query.setProvinceCode(provice.getProvinceCode());
//            List<DicCityCode> cityList= cityCodeDao.selectListMapper( null);
            List<DicCityCode> cityList = cityCodeDao.selectList(new EntityWrapper<DicCityCode>().eq("province_code", provice.getProvinceCode()));
            for (DicCityCode city : cityList) {
//                DicDistrictCode district_query = new DicDistrictCode();
//                district_query.setCityCode(city.getCityCode());
//                List<DicDistrictCode> districtList= districtCodeDao.selectListMapper(null);
                List<DicDistrictCode> districtList = districtCodeDao.selectList(new EntityWrapper<DicDistrictCode>().eq("city_code", city.getCityCode()));
                city.setDistrictList(districtList);
            }
            provice.setCityList(cityList);
        }
//		RedisClient.getConnect().set("allArea".getBytes(), SerializeUtil.serialize(provinceList));
        RedisClient.putListToRedisOfByte(RedisClient.ALL_AREA, provinceList);
        return new JsonForList(provinceList, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }


    @Override
    public JsonForList getProviceAndCityByPinyin() {
        return null;
    }

    @Override
    public String getPcdNameByDistrictCodeDao(String districtCode) {
        if (StringUtils.isBlank(districtCode)) {
            return null;
        }
        List<DicDistrictCode> districtList = districtCodeDao.selectList(new EntityWrapper<DicDistrictCode>().eq("district_code", districtCode));
        if (districtList == null || districtList.size() == 0) {
            return null;
        }
        List<DicCityCode> cityList = cityCodeDao.selectList(new EntityWrapper<DicCityCode>().eq("city_code", districtList.get(0).getCityCode()));
        if (cityList == null || cityList.size() == 0) {
            return districtList.get(0).getDistrictName();
        }
        List<DicProvinceCode> provinceList = provinceCodeDao.selectList(new EntityWrapper<DicProvinceCode>().eq("province_code", cityList.get(0).getProvinceCode()));
        if (provinceList == null || provinceList.size() == 0) {
            return cityList.get(0).getCityName() + "|" + districtList.get(0).getDistrictName();
        }
        return provinceList.get(0).getProvinceName() + "|" + cityList.get(0).getCityName() + "|" + districtList.get(0).getDistrictName();
    }

    public static void main(String args[]) {
        String str = "1234567890111";
        int n = 4;
        System.out.println(str.substring(str.length() - n));

        String licenseNo = "aaa|qwefqw| ";
        String[] sss = licenseNo.split("\\|");
        System.out.println(sss.length);
        if (sss != null && sss.length == 3) {
            System.out.println(sss[0]);
            System.out.println(sss[1]);
            System.out.println(sss[2]);
        }
        if(licenseNo.contains("|")){
            System.out.println("sdasdf");
        }

    }
}
